package com.yuda.tools.log.utils;


import com.yuda.tools.log.entity.ErrorLog;
import com.yuda.tools.log.entity.LogInfo;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExcelUtil {
	
	public static void export(List<LogInfo> list, OutputStream os) throws IOException {
		String[] titles = {"时间","科目","题目ID","题号","操作","信息","结果"};
		XSSFWorkbook workbook = new XSSFWorkbook();
		XSSFSheet sheet = workbook.createSheet();
		XSSFRow rowTitle = sheet.createRow(0);
		for(int i=0; i<titles.length; i++) {
			XSSFCell cellTitle = rowTitle.createCell(i);
			cellTitle.setCellValue(titles[i]);
		}
		for(int i=0; i<list.size(); i++) {
			XSSFRow row = sheet.createRow(i+1);
			for(int j=0; j<titles.length; j++) {
				XSSFCell cell = row.createCell(j);
				if(j == 0) {
					cell.setCellValue(list.get(i).getTime());
				}else if (j == 1) {
					cell.setCellValue(list.get(i).getSubjectName());
				}else if (j == 2) {
					cell.setCellValue(list.get(i).getQuesId());
				}else if (j == 3) {
					cell.setCellValue(list.get(i).getQuestionNo() == null?0:list.get(i).getQuestionNo());			
				}else if (j == 4) {
					cell.setCellValue(list.get(i).getStatType());
				}else if (j == 5) {
					cell.setCellValue(list.get(i).getAnswerAbc());
				}else if (j == 6) {
					cell.setCellValue(list.get(i).getResult());
				}
			}
		}
		workbook.write(os);
		os.flush();
		os.close();
	}
	
	/**
	 * 
	 * @param list 需要导出的数据集合
	 * @param os 输出流
	 * @param map key:实体字段名 value:标题别名
	 * @throws IOException
	 * @throws Exception
	 */
	public static void export(List<?> list, OutputStream os, Map<String, String> map) throws Exception {
		XSSFWorkbook wb = new XSSFWorkbook();
		XSSFSheet sheet = wb.createSheet();
		XSSFRow rowTitle = sheet.createRow(0);
		List<String> methodNames = new ArrayList<>();
		List<String> alias = new ArrayList<>();
		map.forEach((k,v)->{
			String methodName = "get"+k.substring(0, 1).toUpperCase()+k.substring(1);
			methodNames.add(methodName);
			alias.add(v);
		});
		for(int i=0; i<alias.size(); i++) {
			XSSFCell cellTitle = rowTitle.createCell(i);
			cellTitle.setCellValue(alias.get(i));
		}
		List<Method> methods = new ArrayList<>();
		if(list.size()>0) {
			Class clazz = list.get(0).getClass();
			methodNames.forEach(p->{
				try {
					Method method = clazz.getDeclaredMethod(p);
				//	System.out.println(method.getName());
					methods.add(method);
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
			for(int i=0; i<list.size(); i++) {
				XSSFRow row = sheet.createRow(i+1);
				for(int j=0; j<alias.size(); j++) {
					XSSFCell cell = row.createCell(j);
					cell.setCellValue(methods.get(j).invoke(list.get(i))==null?"":methods.get(j).invoke(list.get(i)).toString());
				}
			}
		}
		wb.write(os);
		os.flush();
		os.close();
	}
	/**
	 * errorLog日志导出
	 * @param errorLogs 需要导出的数据
	 * @param fos 
	 * @throws Exception
	 */
	public static void downloadErrorLog(List<ErrorLog> errorLogs, FileOutputStream fos) throws Exception {
		 XSSFWorkbook hwb = new XSSFWorkbook();// 第一步，创建一个workbook（一个excel文件）
		 XSSFSheet hs = hwb.createSheet("表格详情信息");// 第二步，在workbook中添加一个sheet，对应excel文件中sheet
		 XSSFRow hr = hs.createRow((int) 0);// 第三部，在sheet中添加表头第0行（相当于解释字段）
		// 1.生成字体对象	
		 XSSFFont font= hwb.createFont();
		 font.setFontName("仿宋_GB2312");
		 font.setFontHeightInPoints((short) 16);//设置字体大小
		  
		  XSSFCellStyle hcs = hwb.createCellStyle();//第四步，设置第0行（表头）居中
		  hcs.setFont(font);
		  
		  
		  // 将表头的字段放入数组当中
		  String[] arr = new String[5];
		  arr[0] = "id";
		  arr[1] = "studentId";
		  arr[2] = "paperId";
		  arr[3] = "subjectId";
		  arr[4] = "ctime";
			  for (int i = 0; i < arr.length; i++) {
				  XSSFCell hc = hr.createCell(i);
				  hc.setCellValue(arr[i]);
				  hc.setCellStyle(hcs);
				  hs.autoSizeColumn(i);
			  }
			  int row =0;
			  for(int x=0;x<errorLogs.size();x++) {
				  row++;
				  hr = hs.createRow(row); //在sheet中自动随 i+1 增加一行（i 是表头）
				  hr.createCell(0).setCellValue(row);
				  hr.createCell(1).setCellValue(errorLogs.get(x).getStudentId());
				  hr.createCell(2).setCellValue(errorLogs.get(x).getPaperId());
				  hr.createCell(3).setCellValue(errorLogs.get(x).getSubjectId());
				/*  String ctime = DateUtil.changeDateFormat();*/
				  hr.createCell(4).setCellValue(errorLogs.get(x).getCtime());
			  }
			  
		
		hwb.write(fos);;//写入
		fos.close();//关闭资源
		System.out.println("导出成功！    无任何异常。");
		
	}

}
